package mall.repository;

import mall.entity.Category;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xf07b1
 * @wechat xf07b1
 */
public class CategoryDao extends BaseDao<Category> {
    @Override
    public Category rowToObject(ResultSet rs) throws Exception {
        Category category = new Category();
        category.setId(rs.getInt("id"));
        category.setName(rs.getString("name"));
        return category;
    }

    public List<Category> selectList(Category category) {
        StringBuilder sb = new StringBuilder("select * from  category where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (category.getId() != null) {
            sb.append(" and id = ?");
            params.add(category.getId());
        }

        if (category.getName() != null) {
            sb.append(" and name like ?");
            params.add("%" + category.getName() + "%");
        }

        return executeQuery(sb.toString(), params.toArray());
    }


    public Category selectById(Integer categoryId) {
        List<Category> list = executeQuery("select * from category where id = ? ", categoryId);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public int updateById(Category category) {
        String sql = "update category set name = ? where id = ?";
        return executeUpdate(sql, category.getName(), category.getId());
    }

    public int insert(Category category) {
        String sql = "insert into `category`(name) values(?)";
        return executeUpdate(sql, category.getName());
    }

    public int deleteById(int id) {
        String sql = "delete from category where id = ?";
        return executeUpdate(sql, id);
    }

    public Integer count(Category category) {
        StringBuilder sb = new StringBuilder("select count(*) from  category where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (category.getId() != null) {
            sb.append(" and id = ?");
            params.add(category.getId());
        }
        if (category.getName() != null) {
            sb.append(" and name like ?");
            params.add("%" + category.getName() + "%");
        }

        return executeQueryCount(sb.toString(), params.toArray());
    }

    public List<Category> selectList(Category category,  Integer offset,Integer size) {
        StringBuilder sb = new StringBuilder("select * from  category where 1=1 ");
        List<Object> params = new ArrayList<>();

        if (category.getId() != null && category.getId() != 0) {
            sb.append(" and id = ?");
            params.add(category.getId());
        }
        if (category.getName() != null && !"".equals(category.getName())) {
            sb.append(" and name like ?");
            params.add("%" + category.getName() + "%");
        }

        sb.append(" limit ?, ?");
        params.add(offset);
        params.add(size);

        return executeQuery(sb.toString(), params.toArray());
    }
}
